/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package diet.dao;

import diet.dto.DietDetailDTO;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 *
 * @author BadBoy
 */
public class DietDetailDAO {

    public boolean add(DietDetailDTO dd) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = DAO.ConnectWithDriver();
            stm = con.prepareStatement("INSERT INTO [DietDetail](DietID,DishID,Time,DateTime) VALUES(?,?,?,?)");
            stm.setInt(1, dd.getDietID());
            stm.setInt(2, dd.getDishID());
            stm.setString(3, dd.getTime());
            stm.setDate(4,new java.sql.Date(dd.getDateTime().getTime()));
            stm.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.println("SQLException Error: " + ex.getMessage());
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("Connection error: " + ex.getMessage());
                }
            } else if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("Statement error " + ex.getMessage());
                }
            }
        }
    }

    public boolean delete(int ddid) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = DAO.ConnectWithDriver();
            stm = con.prepareStatement("DELETE FROM [DietDetail] WHERE DietDetailID=? ");
            stm.setInt(1, ddid);
            stm.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.println("SQLException Error: " + ex.getMessage());
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("Connection error: " + ex.getMessage());
                }
            } else if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("Statement error " + ex.getMessage());
                }
            }
        }
    }

    public boolean update(DietDetailDTO dd) {
        Connection con = null;
        PreparedStatement stm = null;
        try {
            con = DAO.ConnectWithDriver();
            stm = con.prepareStatement("UPDATE [DietDetail] SET DietID=?, DishID=?,Time=?,DateTime=? "
                    + " WHERE DietDetailID=?");
            stm.setInt(1, dd.getDietID());
            stm.setInt(2, dd.getDishID());
            stm.setString(3, dd.getTime());
            stm.setDate(4, (Date) dd.getDateTime());
            stm.executeUpdate();
            return true;
        } catch (SQLException ex) {
            System.out.println("SQLException Error: " + ex.getMessage());
            return false;
        } finally {
            if (con != null) {
                try {
                    con.close();
                } catch (SQLException ex) {
                    System.out.println("Connection error: " + ex.getMessage());
                }
            } else if (stm != null) {
                try {
                    stm.close();
                } catch (SQLException ex) {
                    System.out.println("Statement error " + ex.getMessage());
                }
            }
        }
    }

    //NhuNHU
    public List<DietDetailDTO> getDietDetail(int dietID) {

        Connection con = null;
        PreparedStatement st = null;
        List<DietDetailDTO> dietDetailList = new ArrayList<DietDetailDTO>();
        try{
            con = DAO.ConnectWithDriver();
            st = con.prepareStatement("SELECT * FROM [DietDetail] WHERE DietID = ?");
            st.setInt(1, dietID);

            ResultSet rs = st.executeQuery();
            DietDetailDTO dietDetail = new DietDetailDTO();
            dietDetail = null;
            while(rs.next()){
                dietDetail = new DietDetailDTO();
                dietDetail.setDietDetailID(rs.getInt("DietDetailID"));
                dietDetail.setDietID(dietID);
                dietDetail.setDishID(rs.getInt("DishID"));
                dietDetail.setTime(rs.getNString("Time"));
                dietDetail.setDateTime(rs.getDate("DateTime"));
                dietDetailList.add(dietDetail);
            }
            rs.close();
            con.close();
            return dietDetailList;
        }catch (SQLException ex){
            System.out.println("SQLException Error: " + ex.getMessage());
            return null;
        }
    }
}
